package com.xuyiwei.xblog.business.article.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xuyiwei.xblog.business.article.result.article.out.TypeDto;
import com.xuyiwei.xblog.core.base.constant.enums.CommonStatusEnum;
import com.xuyiwei.xblog.core.node.ZTreeNode;
import com.xuyiwei.xblog.business.article.dao.TypeMapper;
import com.xuyiwei.xblog.business.article.model.Type;
import com.xuyiwei.xblog.business.article.service.ITypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 文章类型表 服务实现类
 * </p>
 *
 * @author xuyiwei
 * @since 2018-10-15
 */
@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements ITypeService {

    @Autowired
    private TypeMapper typeMapper;

    @Override
    public List<ZTreeNode> tree() {
        return this.baseMapper.tree();
    }

    @Override
    public void deleteTypeById(Long id) {
        Type type = selectById(id);
        if(type!=null){
            type.setStatus(CommonStatusEnum.DELETE.getKey());
            type.updateById();
        }
    }

    @Override
    public void deleteTypeByIds(List<Long> ids) {
        for(Long id:ids){
            deleteTypeById(id);
        }
    }

    @Override
    public List<Type> listParent() {
        List<Type> list = typeMapper.listParent();
        return getTypes(list);
    }

    @Override
    public List<TypeDto> listTypeForMenu() {
        List<TypeDto> entityList = typeMapper.listTypeForMenu();
        return getTypeDtos(entityList);
    }

    private List<Type> getTypes(List<Type> list) {
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        List<Type> boList = new ArrayList<>();
        for (Type type : list) {
            boList.add(type);
        }
        return boList;
    }


    private List<TypeDto> getTypeDtos(List<TypeDto> list) {
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        List<TypeDto> boList = new ArrayList<>();
        for (TypeDto type : list) {
            boList.add(type);
        }
        return boList;
    }
}
